#include "XmlSaxWriter.h"
#include <string.h>


genxStatus saxSend(void * userData, constUtf8 s)
{
	XmlSaxSender* sender = (XmlSaxSender*)userData;
	return sender->Send(s,strlen((const char*)s));
}

genxStatus saxSendBounded(void * userData, constUtf8 start, constUtf8 end)
{
	XmlSaxSender* sender = (XmlSaxSender*)userData;
	return sender->Send(start,end-start);
}

genxStatus saxFlush(void * userData)
{
	XmlSaxSender* sender = (XmlSaxSender*)userData;
	return sender->Flush();
}

XmlSaxWriter::XmlSaxWriter(XmlSaxSender* sender)
	:m_sender(sender)
{
	m_writer = genxNew(NULL, NULL, sender);
	m_genxSender.flush = saxFlush;
	m_genxSender.send = saxSend;
	m_genxSender.sendBounded = saxSendBounded;
	genxStartDocSender(m_writer, &m_genxSender);
}

XmlSaxWriter::XmlSaxWriter(FILE* sender)
	:m_sender(NULL)
{
	m_writer = genxNew(NULL, NULL, sender);
	genxStartDocFile(m_writer, sender);
}

XmlSaxWriter::~XmlSaxWriter()
{
	genxDispose(m_writer);
}


genxStatus XmlSaxWriter::EndDocument()
{
	return genxEndDocument(m_writer);
}

genxStatus XmlSaxWriter::Comment(const char* text)
{
	return genxComment(m_writer,(constUtf8) text);
}


genxStatus XmlSaxWriter::StartElement(const char* xmlns, const char* type)
{
	return genxStartElementLiteral(m_writer, (constUtf8)xmlns, (constUtf8)type);
}

genxStatus XmlSaxWriter::AddAttribute(const char* xmlns, const char* name, const char* value)
{
	return genxAddAttributeLiteral(m_writer, (constUtf8)xmlns, (constUtf8)name, (constUtf8)value);
}

genxStatus XmlSaxWriter::EndElement()
{
	return genxEndElement(m_writer);
}


genxStatus XmlSaxWriter::AddText(const char* start, int byteCount)
{
	return genxAddCountedText(m_writer, (constUtf8)start, byteCount);
}

const char* XmlSaxWriter::LastError()
{
	return genxLastErrorMessage(m_writer);
}







